package com.shishuo.cms.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.shishuo.cms.constant.GoodsExceptionConstant;
import com.shishuo.cms.dao.UserCallBackGoodsDao;
import com.shishuo.cms.entity.UserCallBackGoods;
import com.shishuo.cms.entity.vo.PageVo;

@Service
public class UserCallBackGoodsService extends BaseService<UserCallBackGoods, Long>
{
	private UserCallBackGoodsDao userCallBackGoodsDao;

	@Autowired
	public void setGoodsItemDao(UserCallBackGoodsDao userCallBackGoodsDao)
	{
		super.setBaseDao(userCallBackGoodsDao);
		this.userCallBackGoodsDao = userCallBackGoodsDao;
	}
	@Autowired
	private CashService cashService;
	@Autowired
	private GoldService goldService;
	/**
	 * 根据用户编号分页获取收藏的产品
	 * 
	 * @param pageNum
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	public PageVo<UserCallBackGoods> getAllListPage(int pageNum, long userId)
																				throws Exception
	{
		PageVo<UserCallBackGoods> pageVo = new PageVo<UserCallBackGoods>(
				pageNum);
		List<UserCallBackGoods> list = userCallBackGoodsDao.getAllList(
				pageVo.getOffset(), pageVo.getRows(), userId);
		pageVo.setList(list);
		pageVo.setCount(userCallBackGoodsDao.getAllListCount(userId));
		return pageVo;
	}

	public void saveUserCallBackGoods(UserCallBackGoods userCallBackGoods) throws Exception
	{
		if (null == userCallBackGoods)
		{
			throw new Exception(GoodsExceptionConstant.SAVE_FAV_GOODS_ERROR_GOODS_IS_NULL);
		}
		if (userCallBackGoodsDao.existUserCallBackGoods(userCallBackGoods.getUserId(), userCallBackGoods.getGoodsId(), userCallBackGoods.getGoodsOrderNum()))
		{
			throw new Exception(GoodsExceptionConstant.SAVE_FAV_GOODS_ERROR_GOODS_ORDER_HAS_POSTED);
		}
		insert(userCallBackGoods);
	}

	/**
	 * 处理返现的订单
	 * 
	 * @since 2015年5月12日 下午2:08:05
	 * @param oldUserCallBackGoods
	 * @throws Exception
	 */
	@Transactional(rollbackFor=java.lang.Exception.class)
	public void dealCallBackOrder(UserCallBackGoods userCallBackGoods) throws Exception
	{
		UserCallBackGoods oldUserCallBackGoods = getById(userCallBackGoods.getId());
		//订单更新
		if (oldUserCallBackGoods.getStatus()!=0)
		{
			throw new Exception(GoodsExceptionConstant.DEAL_GOODS__ORDER_ERROR_ORDER_HAS_DEALED);
		}
		if(userCallBackGoods.getStatus()==1){
			cashService.addMoneyByCallBack(oldUserCallBackGoods.getUserId(),userCallBackGoods.getCallBackMoney(),0,userCallBackGoods.getGoodsOrderNum());
			goldService.addGoldByCallBack(oldUserCallBackGoods.getUserId(),userCallBackGoods.getCallBackGold(),0,userCallBackGoods.getGoodsOrderNum());
		}
		oldUserCallBackGoods.setCallBackMoney(userCallBackGoods.getCallBackMoney());
		oldUserCallBackGoods.setCallBackGold(userCallBackGoods.getCallBackGold());
		oldUserCallBackGoods.setStatus(userCallBackGoods.getStatus());
		oldUserCallBackGoods.setPassMsg(userCallBackGoods.getPassMsg());
		update(oldUserCallBackGoods);
	}
}
